package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.Constants;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.AccountConfig;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.AccountConfigService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.impl.SensitivenessServiceImpl;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * @author chenmengran
 * @Description: 运营区域配置
 * @date 2020\1\4 000410:46
 */
@Validated
@RestController
@RequestMapping(value = "/accountConfig", produces = BaseController.MEDIA_JSON_UTF8)
public class AccountConfigController extends BaseController {
    @Autowired
    private AccountConfigService accountConfigService;
    @Autowired
    private SensitivenessServiceImpl sensitivenessService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private OperaPswService operaPswService;

    //运营区域配置
    @RequestMapping("/edit")
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.ACCOUNT_CONFIG_EDIT)
    public JsonResult edit(AccountConfig accountConfig, String operaPsw, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountConfig.setAccountId(loginInfo.id);
        } else {
            Assert.notNull(accountConfig.getAccountId(), "ver.accountId.notNull");
        }
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), operaPsw);

        accountConfigService.edit(loginInfo, accountConfig);
        redisService.del(Constants.ACCOUNTCONFIG_KEY_PREFIX + accountConfig.getAccountId());
        sensitivenessService.sendMessage(accountConfig.getAccountId(),2);
        return JsonResult.ok();
    }

    /**
     * @描述 获取运营区域配置
     */
    @RequestMapping("/getByAccount")
    public JsonResult getByAccount(@NotNull(message = "ver.accountId.notNull") Integer accountId, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);

        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }
        AccountConfig accountConfig = accountConfigService.getByAccountId(loginInfo, accountId);
        /**为空生成默认配置*/
        if (accountConfig == null) {
            accountConfig = new AccountConfig();
            //平台运营开关
            accountConfig.setServiceSwitch(1);
            accountConfig.setAccountId(accountId);
            //自动锁车开关
            accountConfig.setAutoLock(1);
            //强制结束订单开关
            accountConfig.setFinishOrderSwitch(1);
            //静止临停时间
            accountConfig.setStopTime(10);
            //静止结束订单时间
            accountConfig.setFinishTime(30);
            //换电阈值
            accountConfig.setSocLimit(50);
            //换电有效值
            accountConfig.setSocLimitIsValid(80);
            //实名认证开关
            accountConfig.setNameAuth(1);
            //学生认证开关
            accountConfig.setStudentAuth(0);
            //多少小时产生订单
            accountConfig.setRideTime(0);
            //两个工单内是否产生订单，0：不产生
            accountConfig.setRide(0);
            //订单距离
            accountConfig.setRideDistance(0);
            //记录间隔时间
            accountConfig.setLogTime(0);
            //垂直停车还车次数
            accountConfig.setReturnTimes(3);
            //人工调度申请自动审核 0关闭 1开启
            accountConfig.setDispatchApplyAutoAuditSwitch(0);
            //人工调度申请上传照片 0关闭 1开启
            accountConfig.setDispatchApplyPhotoSwitch(0);
            //运维开锁推车 车辆静止自动锁车时长
            accountConfig.setPushMachineAutoLockTime(30);

            accountConfigService.insert(accountConfig);
        }
        return JsonResult.ok(accountConfig);
    }
}
